﻿(function () {
    "use strict";
    var fileManageModule = angular.module('ui.file.manage', []);

    fileManageModule.controller('fileManageCtrl', function ($scope, $uibModal, $uibModalInstance, utils, baseService, bind_Data) {
        $scope.query = { page: 1, size: 12, category_id: null };
        $scope.files = {};
        $scope.show_upload = false;
        $scope.psdisabled = false;
        $scope.upload_disabled = false;
        $scope.web_image_url = '';
        $scope.uploadfiles = [];
        $scope.image_paths = bind_Data.image_paths;
        $scope.upload_path = bind_Data.upload_path;
        $scope.limit = bind_Data.limit;

        var methods = {
            search: function () {
                baseService.get_file($scope.query).success(function (response) {
                    $scope.files = response.data;
                });
            },
            get_file_category: function () {
                baseService.get_file_category().success(function (response) {
                    $scope.file_categorys = response.data;
                });
            },
            ok: function () {
                $uibModalInstance.close($scope.image_paths);
            },
            cancel: function () {
                $uibModalInstance.dismiss('cancel');
            },
            choose: function (file_url) {
                var index = $scope.image_paths.indexOf(file_url);
                if (index != -1)
                    $scope.image_paths.splice(index, 1);
                else {
                    if ($scope.limit == 1) {
                        $scope.image_paths = [];
                        $scope.image_paths.push(file_url);
                    }
                    else {
                        if ($scope.image_paths.length == $scope.limit)
                            utils.notify('最多可以选择' + $scope.limit + '张图片', "warning");
                        else
                            $scope.image_paths.push(file_url);
                    }
                }
            },
            delete_file: function (file_url) {
                var index = $scope.uploadfiles.indexOf(file_url);
                if (index != -1)
                    $scope.uploadfiles.splice(index, 1);
            },
            upload_files: function () {
                $scope.upload_disabled = true;
                baseService.upload_file({ id: $scope.upload_path, file: $scope.uploadfiles }).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.upload_disabled = false;
                    $scope.uploadfiles = [];
                    if (response.result_code == 1000) {
                        if ($scope.limit == 1) {
                            $scope.image_paths = [];
                            $scope.image_paths.push(response.data[0]);
                        }
                        else {
                            if ($scope.image_paths.length < $scope.limit) {
                                $.each(response.data, function (i, file_url) {
                                    $scope.image_paths.push(file_url);
                                });
                            }
                        }
                    }
                }).error(function () {
                    $scope.upload_disabled = false;
                    $scope.uploadfiles = [];
                });
            },
            upload_web_file: function () {
                $scope.psdisabled = true;
                baseService.upload_web_file({ id: $scope.upload_path, file_url: $scope.web_image_url }).success(function (response) {
                    utils.notify(response.result_msg, response.result_code == 1000 ? "success" : "warning");
                    $scope.psdisabled = false;
                    $scope.web_image_url = '';
                    if (response.result_code == 1000) {
                        if ($scope.limit == 1) {
                            $scope.image_paths = [];
                            $scope.image_paths.push(response.data);
                        }
                        else {
                            if ($scope.image_paths.length < $scope.limit) {
                                $scope.image_paths.push(response.data);
                            }
                        }
                    }
                }).error(function () {
                    $scope.psdisabled = false;
                    $scope.web_image_url = '';
                });
            }
        };
        $.extend($scope, methods);
        methods.get_file_category();
        methods.search();
    });

    fileManageModule.directive('fileManage', function ($uibModal) {
        return {
            restrict: 'E',
            require: '?ngModel',
            link: function (scope, element, attrs, ngModel) {
                var ele = element[0];
                var limit = attrs['limit'];
                var upload_path = attrs['upload'];
                var upload_btn = $('.upload', ele);
                if (upload_btn.length == 0)
                    upload_btn = upload_btn.prevObject;

                upload_btn.click(function () {
                    var image_paths = [];
                    if (limit == 1)
                        image_paths.push(ngModel.$viewValue);
                    else {
                        $.each(ngModel.$viewValue, function (index, item) {
                            image_paths.push(item);
                        });
                    }
                    open(image_paths);
                });

                var open = function (image_paths) {
                    var modalInstance = $uibModal.open({
                        templateUrl: '/tpl/choose_image.html',
                        backdrop: "static",
                        controller: "fileManageCtrl",
                        windowClass: "choose_image_modal_w",
                        resolve: {
                            bind_Data: function () {
                                return { image_paths: image_paths, limit: limit, upload_path: upload_path };
                            }
                        }
                    });
                    modalInstance.result.then(function (image_paths) {
                        updateModel(image_paths);
                    });
                }

                var updateModel = function (image_paths) {
                    if (limit == 1)
                        ngModel.$setViewValue(image_paths.length > 0 ? image_paths[0] : '');
                    else
                        ngModel.$setViewValue(image_paths);
                };
            }
        };
    });
}());
